package com.ruoyi.web.controller.system;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.HatchetMerchant;
import com.ruoyi.system.domain.HatchetMerchantIncreaseLimit;
import com.ruoyi.system.domain.HatchetPlanItem;
import com.ruoyi.system.service.IHatchetMerchantIncreaseLimitService;
import com.ruoyi.system.service.IHatchetMerchantService;
import com.ruoyi.system.service.IHatchetPlanItemService;
import com.ruoyi.framework.web.base.BaseController;
import com.ruoyi.common.page.TableDataInfo;
import com.ruoyi.common.base.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 还款计划明细 信息操作处理
 * 
 * @author ruoyi
 * @date 2019-03-02
 */
@Controller
@RequestMapping("/system/hatchetPlanItem")
public class HatchetPlanItemController extends BaseController
{
    private String prefix = "system/hatchetPlanItem";
	
	@Autowired
	private IHatchetPlanItemService hatchetPlanItemService;
	@Autowired
	private IHatchetMerchantIncreaseLimitService increaseLimitService;
	@Autowired
	private IHatchetMerchantService hatchetMerchantService;
	@RequiresPermissions("system:hatchetPlanItem:view")
	@GetMapping()
	public String hatchetPlanItem()
	{
	    return prefix + "/hatchetPlanItem";
	}
	
	/**
	 * 查询还款计划明细列表
	 */
	@RequiresPermissions("system:hatchetPlanItem:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(HatchetPlanItem hatchetPlanItem)
	{
		//判断商户条件是否为空
		if (StringUtils.isNotEmpty(hatchetPlanItem.getMerchantName())
				||StringUtils.isNotEmpty(hatchetPlanItem.getMerchantNo())) {
			HatchetMerchant paramMerchant = new HatchetMerchant();
			paramMerchant.setMerchantCnName(hatchetPlanItem.getMerchantName());
			paramMerchant.setMerchantNo(hatchetPlanItem.getMerchantNo());
			List<HatchetMerchant> merchantList = hatchetMerchantService.selectHatchetMerchantList(paramMerchant);
			if (merchantList != null && merchantList.size()>0) {
				HatchetMerchant hatchetMerchant = merchantList.get(0);
				hatchetPlanItem.setMerchantId(hatchetMerchant.getID());
			}
		}
		startPage();
        List<HatchetPlanItem> list = hatchetPlanItemService.selectHatchetPlanItemList(hatchetPlanItem);
        for (HatchetPlanItem hatchetPlanItem2 : list) {
        	//1.封装商户信息
        	HatchetMerchant merchant = hatchetMerchantService.selectHatchetMerchantById(hatchetPlanItem2.getMerchantId());
        	if (StringUtils.isNotNull(merchant)) {
        		hatchetPlanItem2.setMerchantNo(merchant.getMerchantNo()); 
        		hatchetPlanItem2.setMerchantName(merchant.getMerchantCnName());
			}
        	//2.封装账户信息
        	HatchetMerchantIncreaseLimit increaseLimit= 
        			increaseLimitService.selectHatchetMerchantIncreaseLimitById(hatchetPlanItem2.getFromIncreaseId());
        	if (StringUtils.isNotNull(increaseLimit)) {
        		hatchetPlanItem2.setFromBankAccount(increaseLimit.getBankAccount());
        		hatchetPlanItem2.setFromBankAccountName(increaseLimit.getBankAccountName());
        		hatchetPlanItem2.setFromBankphone(increaseLimit.getBankPhone());
			}
		}
        return getDataTable(list);
	}
	
	
	/**
	 * 导出还款计划明细列表
	 */
	@RequiresPermissions("system:hatchetPlanItem:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(HatchetPlanItem hatchetPlanItem)
    {
    	List<HatchetPlanItem> list = hatchetPlanItemService.selectHatchetPlanItemList(hatchetPlanItem);
        ExcelUtil<HatchetPlanItem> util = new ExcelUtil<HatchetPlanItem>(HatchetPlanItem.class);
        return util.exportExcel(list, "hatchetPlanItem");
    }
	
	/**
	 * 新增还款计划明细
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存还款计划明细
	 */
	@RequiresPermissions("system:hatchetPlanItem:add")
	@Log(title = "还款计划明细", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(HatchetPlanItem hatchetPlanItem)
	{		
		return toAjax(hatchetPlanItemService.insertHatchetPlanItem(hatchetPlanItem));
	}

	/**
	 * 修改还款计划明细
	 */
	@GetMapping("/edit/{iD}")
	public String edit(@PathVariable("iD") String iD, ModelMap mmap)
	{
		HatchetPlanItem hatchetPlanItem = hatchetPlanItemService.selectHatchetPlanItemById(iD);
		mmap.put("hatchetPlanItem", hatchetPlanItem);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存还款计划明细
	 */
	@RequiresPermissions("system:hatchetPlanItem:edit")
	@Log(title = "还款计划明细", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(HatchetPlanItem hatchetPlanItem)
	{		
		return toAjax(hatchetPlanItemService.updateHatchetPlanItem(hatchetPlanItem));
	}
	
	/**
	 * 删除还款计划明细
	 */
	@RequiresPermissions("system:hatchetPlanItem:remove")
	@Log(title = "还款计划明细", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(hatchetPlanItemService.deleteHatchetPlanItemByIds(ids));
	}
	
}
